package com.zysd.collection.scheduler.service.common;

import com.lensyn.collect.common.enums.ProtocolEnum;
import com.lensyn.common.utils.service.common.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 通过redis获取终端版本型号
 *
 * @author hetao
 */
@SuppressWarnings("unchecked")
@Service
public class ProtocolEnumService {

    @Autowired
    private RedisService redisService;

    @Value("${spring.rabbitmq.virtual-host}")
    private String appName;

    /**
     * 获取终端版本号
     * @param region region
     * @param local local
     * @return ProtocolEnum
     */
    public ProtocolEnum convert(int region, int local) {
        String key = ProtocolEnum.generateKey(appName, region, local);
        String code = redisService.get(key);
        return ProtocolEnum.getByCode(code);
    }

    /**
     * 将对应终端版本号添加到 redis
     * @param region region
     * @param local local
     * @param protocol 版本号
     */
    public void set(Integer region ,Integer local ,String  protocol){
        if (region == null || local == null || StringUtils.isEmpty(protocol)) {
           return;
        }
        String key = ProtocolEnum.generateKey(appName, region, local);
        redisService.getTemplate().opsForValue().set(key, protocol);
    }
}
